﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using RobotCtrl;
using RobotView;
using RobotIO;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace DayTimeServerTest
{

    public partial class Form1 : Form
    {

        #region members
        private Robot robot;
        private Thread dtTh; // DayTimeServer Thread
        #endregion

        #region constructor & destructor
        public Form1()
        {
            InitializeComponent();
            robot = new Robot();
            consoleView1.RobotConsole = robot.RobotConsole;

            dtTh = new Thread(DayTimeServerHandler);
            dtTh.Start();
            
        }
        #endregion

        #region methods
        public void DayTimeServerHandler()
        {
            TcpListener listen = new TcpListener(IPAddress.Any, 13);
            listen.Start();
            while (true)
            {
                Console.WriteLine("Warte auf Verbindung auf Port " +
                    listen.LocalEndpoint + "...");
                TcpClient client = listen.AcceptTcpClient();
                robot.RobotConsole[Leds.Led1].LedEnabled = true;
                Console.WriteLine("Verbindung zu " + client.Client.RemoteEndPoint);
                TextWriter tw = new StreamWriter(client.GetStream());
                tw.Write(DateTime.Now.ToString());
                tw.Flush();
                Thread.Sleep(3000);
                client.Close();
                robot.RobotConsole[Leds.Led1].LedEnabled = false;
            }
        }
        #endregion
    }
}